package com.tanhua.server.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.QuestionApi;
import com.tanhua.dubbo.server.api.SoulApi;
import com.tanhua.dubbo.server.pojo.Question;
import com.tanhua.dubbo.server.pojo.Soul;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.SoulVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("testSoul")//testSoul
public class SoulController {

    @Reference(version = "1.0.0")
    private SoulApi soulApi;

    @Reference(version = "1.0.0")
    private QuestionApi questionApi;


    /**
     * 提交问卷
     */
    @PostMapping
    public ResponseEntity<Void> commit(@RequestBody Map<String, List<Map<String, String>>> data) {

        List<Map<String, String>> answers = data.get("answers");
        answers.forEach(map -> {
            System.out.println("questionId:" + map.get("questionId"));
            System.out.println("optionId:" + map.get("optionId"));
            System.out.println("================================");
        });

        return ResponseEntity.ok(null);
    }


    /**
     * 问卷列表
     */
    @GetMapping
    public ResponseEntity<Object> list() {
        List<Soul> list = soulApi.findByUserId(UserThreadLocal.get().getId().intValue());

        List<SoulVo> soulVoList = new ArrayList<>();
        list.forEach(soul -> {

            SoulVo soulVo = new SoulVo();

            List<Question> questionList = questionApi.findByIds(soul.getQuestionIds());
            soulVo.setId(soul.getId().toHexString());
            soulVo.setName(soul.getName());
            soulVo.setStar(soul.getStar());
            soulVo.setCover(soul.getCover());
            soulVo.setLevel(soul.getLevel());
            soulVo.setIsLock(soul.getIsLock());
            soulVo.setQuestions(questionList);
            soulVo.setReportId(soul.getReportId());

            soulVoList.add(soulVo);
        });
        return ResponseEntity.ok(soulVoList);
    }
}
